package com.sosky;

import java.util.*;

/**
 * 17. 电话号码的字母组合
 * <p>
 * 给定一个仅包含数字 2-9 的字符串，返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
 * 给出数字到字母的映射如下（与电话按键相同）。注意 1 不对应任何字母。
 * <p>
 * 示例 1：
 * 输入：digits = "23"
 * 输出：["ad","ae","af","bd","be","bf","cd","ce","cf"]
 * <p>
 * 示例 2：
 * 输入：digits = ""
 * 输出：[]
 * <p>
 * 示例 3：
 * 输入：digits = "2"
 * 输出：["a","b","c"]
 * <p>
 * 提示：
 * 0 <= digits.length <= 4
 * digits[i] 是范围 ['2', '9'] 的一个数字。
 */
public class Solution17 {
    public static void main(String[] args) {
        System.out.println(letterCombinations("23"));
        System.out.println(letterCombinations(""));
        System.out.println(letterCombinations("2"));
    }

    public static List<String> letterCombinations(String digits) {
        List<String> result = new ArrayList<>();
        if (digits != null && digits.length() > 0) {
            Map<Character, String[]> mapper = new HashMap<>();
            mapper.put('2', new String[]{"a", "b", "c"});
            mapper.put('3', new String[]{"d", "e", "f"});
            mapper.put('4', new String[]{"g", "h", "i"});
            mapper.put('5', new String[]{"j", "k", "l"});
            mapper.put('6', new String[]{"m", "n", "o"});
            mapper.put('7', new String[]{"p", "q", "r", "s"});
            mapper.put('8', new String[]{"t", "u", "v"});
            mapper.put('9', new String[]{"w", "x", "y", "z"});
            char[] chars = digits.toCharArray();
            List<String[]> didi = new ArrayList<>();
            for (int i = 0; i < chars.length; i++) {
                String[] alphas = mapper.get(chars[i]);
                if (result.size() == 0) {
                    result.addAll(Arrays.asList(alphas));
                } else {
                    List<String> newList = new ArrayList<>();
                    for (int j = 0; j < result.size(); j++) {
                        for (int k = 0; k < alphas.length; k++) {
                            newList.add(result.get(j) + alphas[k]);
                        }
                    }
                    result = newList;
                }
            }
        }
        return result;
    }
}
